<?php

class Photo extends Article {

	var $id_photo;
	var $title;
	var $description;
	var $link;
	var $thumbnail;
	var $extensions;
	
	/////////////////////////////////////////////////
	// Static member methods
	
	function Photo($id_photo = -1) {
		global $site;
		$this->_table_name = "articles";
		$this->_site = $site;
		$this->_object_type = "photo";
		$this->_db = new mysqlconn(HOST,USER,PASS,DB);
		$this->_meta_fields = array("link","thumbnail");		
		$this->extensions = array("jpg","jpeg","gif","png","jpe");
		$this->_alias_fields = array("id_article" => "id_photo", "title" => "title", "content" => "description");

		if ($id_photo != -1) {
			$this->load($id_photo);
		}
	}
	
	function create($title, $description, $file, $id_parent) {
		$photo = new Photo();
		$photo->title = $title;
		$photo->id_parent = $id_parent;
		$photo->description = $description;	
		return $photo->save_photo($file);
	}

	//////////////////////////////////////////////////////////////////////////////
	// Instance member methods
	function _create_thumbnail($path) {

	  $path_splitted = split("/", $path);
	  $thumbAux = $path_splitted[count($path_splitted) - 1];
	  $thumbnail_filename = "tn_" . str_replace(".","_",$thumbAux) . ".jpg";
	  
	  
      // load image and get image size
      $img = imagecreatefromjpeg( $path);
      $width = imagesx( $img );
      $height = imagesy( $img );

      // calculate thumbnail size
      $new_width = 96;
      $new_height = 72;

      // create a new temporary image
      $tmp_img = imagecreatetruecolor( $new_width, $new_height );

      // copy and resize old image into new image
      imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height );
	  
      // save thumbnail into a file
      imagejpeg( $tmp_img, "+photo/uploads/".$thumbnail_filename);
	  $this->thumbnail = "+photo/uploads/".$thumbnail_filename;
	}
	
	function save_photo($file) {
		
		$valid = 0; //It's used to control that the file has a valid file extension.
		$root_path = "./+photo/uploads/";
		$filename = basename($file['name']);
		$target_path = $root_path . $filename; //This is used for the big image.
		//Now, we are checking if the file is valid.
		$arr = split("\.",$filename);
		foreach($this->extensions as $ext) {
			if(strtolower($arr[count($arr)-1]) == strtolower($ext)) {
				$valid = 1;
			}
		}

		if(!$valid) {
			$this->_errmsg = Site::lang("Invalid file extension, it should a valid web image file");
			return 0;
		}
		
		//First we check that the file isn't larger than the maximum file size accepted.		
		if ($file['size'] >= $this->_site->config['photo.max_file_size']) {
			$this->_errmsg = Site::lang("File too large");
					return 0;
		}
		
		//Now we are moving the temporary file to it's final destination
		if(!move_uploaded_file($file['tmp_name'], $target_path)) {
			$this->_errmsg = Site::lang("The image wasn't correctly uploaded");
			return 0;
		}
		$this->link = $target_path;
		
		$this->_create_thumbnail($target_path);
		return $this->save();		
	}
	
	function get_sons() {
	
		// If sons is not loaded, bring it forth from the DB
		if (!$this->sons) {
			
			$this->sons = array();
			
			$this->_db->query(
				"SELECT
					id_article
				FROM
					".PREFIX."articles
				WHERE
					id_parent = '?'
					AND type = 'comment'",
					$this->get_id());
			while ($a = $this->_db->getfield()) {
				array_push($this->sons, new Comment($a));
			}
		}
		
		return $this->sons;
	}
	
	function get_comments() {
		return $this->get_sons();
	}
	
	// Overwriten from Article
	function delete() {
		$this->load($this->get_id());
		$del = unlink($this->link);
		$del = unlink("./".$this->thumbnail);
		parent::delete();
		return 1;
	}
	
	///////////////////////////////////////////////////////////////////////////////
	// Polymorphism methods
	function get_instance($id_instance) {
		return new Photo($id_instance);
	}

	
}

?>